package org.movee.jalgo.search;

/**
 * 二分查找
 */
public class BinarySearch<K extends Comparable<K>> {

    /**
     * 查找指定的key
     * @param arr 数组
     * @param key 查找key
     * @return 存在：key的索引；不存在：-1
     */
    public int search(K[] arr, K key) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int low = 0;
        int high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            int cmp = arr[mid].compareTo(key);
            if (cmp < 0) {
                low = mid + 1;
            } else if (cmp > 0) {
                high = mid - 1;
            } else {
                return mid;
            }
        }

        return -1;
    }

    /**
     * 查找指定的key，返回第一个
     * @param arr 数组
     * @param key 查找key
     * @return 存在：第一个key的索引；不存在：-1
     */
    public int searchFirst(K[] arr, K key) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int highest = arr.length - 1;
        int low = 0;
        int high = highest;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            int cmp = arr[mid].compareTo(key);
            if (cmp < 0) {
                low = mid + 1;
            } else if (cmp > 0) {
                high = mid - 1;
            } else {
                if (mid == 0 || arr[mid - 1].compareTo(key) != 0) {
                    // 已经是数组中第一个，或者左边的数不等于key，直接返回
                    return mid;
                } else {
                    // 继续在左边寻找
                    high = mid - 1;
                }
            }
        }

        return -1;
    }

    /**
     * 查找指定的key，返回最后一个
     * @param arr 数组
     * @param key 查找key
     * @return 存在：第一个key的索引；不存在：-1
     */
    public int searchLast(K[] arr, K key) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int highest = arr.length - 1;
        int low = 0;
        int high = highest;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            int cmp = arr[mid].compareTo(key);
            if (cmp < 0) {
                low = mid + 1;
            } else if (cmp > 0) {
                high = mid - 1;
            } else {
                if (mid == highest || arr[mid + 1].compareTo(key) != 0) {
                    // 已经是数组中最后一个，或者右边的数不等于key，直接返回
                    return mid;
                } else {
                    // 继续在右边寻找
                    low = mid + 1;
                }
            }
        }

        return -1;
    }

    /**
     * 查找第一个大于等于给定值的元素
     * @param arr 数组
     * @param key 查找key
     * @return 存在：第一个key的索引；不存在：-1
     */
    public int searchFirstGreaterThan(K[] arr, K key) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int highest = arr.length - 1;
        int low = 0;
        int high = highest;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            int cmp = arr[mid].compareTo(key);
            if (cmp < 0) {
                low = mid + 1;
            } else {
                if (mid == 0 || arr[mid - 1].compareTo(key) < 0) {
                    // 已经是数组中第一个，或者左边的数小于key，直接返回
                    return mid;
                } else {
                    // 继续在左边寻找
                    high = mid - 1;
                }
            }
        }

        return -1;
    }

    /**
     * 查找最后一个小于等于给定值的元素
     * @param arr 数组
     * @param key 查找key
     * @return 存在：第一个key的索引；不存在：-1
     */
    public int searchLastLessThan(K[] arr, K key) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int highest = arr.length - 1;
        int low = 0;
        int high = highest;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            int cmp = arr[mid].compareTo(key);
            if (cmp > 0) {
                high = mid - 1;
            } else {
                if (mid == highest || arr[mid + 1].compareTo(key) > 0) {
                    // 已经是数组中最后一个，或者右边的数大于key，直接返回
                    return mid;
                } else {
                    // 继续在右边寻找
                    low = mid + 1;
                }
            }
        }

        return -1;
    }
}
